#include <array>
#include <iostream>
#include <fstream>
#include <memory>

#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

#include <terms/dl/dlterms.hpp>

int
main(int /*argc*/, char *argv[])
{
	CppUnit::TestResult controller;
	CppUnit::TestResultCollector result;
	controller.addListener(&result);

	std::string app_name(argv[0]);
	std::size_t pos = app_name.find("/");
	if (pos != std::string::npos) {
		app_name = app_name.substr(pos + 1, std::string::npos);
	}
	std::string xml_name(app_name + ".xml");

	std::ofstream xml_out(xml_name);


	
	CppUnit::XmlOutputter xmlOutputter(&result, xml_out, "utf-8");
	CppUnit::CompilerOutputter consoleOutputter(&result, std::cout, "utf-8");

	CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
	CppUnit::Test *suite = registry.makeTest();

	std::unique_ptr<CppUnit::TextTestRunner> runner = std::unique_ptr<CppUnit::TextTestRunner>(new CppUnit::TextTestRunner());
	runner->addTest(suite);
	runner->run(controller);

	xmlOutputter.write();
	consoleOutputter.write();
	
	return 0;
}
